package org.dydl.controller.system;

import java.util.List;
import java.util.Map;

import org.dydl.common.dto.ResponseMessage;
import org.dydl.model.system.SysRole;
import org.dydl.service.system.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

// TODO: Auto-generated Javadoc
//import lombok.Setter;

/**
 * The Class RoleController.
 */
@RestController
@RequestMapping("/api/role")
public class RoleController {

	// @Autowired
	// private ISysRoleService roleService;
	// @Autowired
	// private ISysRightsService rightsService;

	@Autowired
	private ISysRoleService sysRoleService;

	/**
	 * 添加角色
	 * 
	 * @param role
	 */
	@RequestMapping("/add")
	public void add(SysRole role) {
		sysRoleService.save(role);
		// roleService.save(role);
	}

	/**
	 * 分页、条件、查询角色
	 *
	 * @return the list
	 */
	@RequestMapping("/getAll")
	public Object list() {
		// ResponseMessage msg = roleService.search(null);
		// return msg;
		return new ResponseMessage(false);
	}

	/**
	 * 分页获取角色
	 */
	@RequestMapping("getRole")
	public List<SysRole> getRole(int pageIndex, int limit, String roleName) {

		return null;
	}

	/**
	 * 根据id获取角色
	 *
	 * @param id
	 *            the id
	 * @return the sys role
	 */
	@RequestMapping("/get")
	public ResponseMessage get(@PathVariable("id") Long id) {
		// return roleService.find(id);
		return new ResponseMessage(false);
	}

	/**
	 * 更新角色
	 *
	 * @param role
	 *            the role
	 * @return the long
	 */
	@RequestMapping("/update")
	public void update(@RequestBody SysRole role) {
		sysRoleService.save(role);
		// roleService.save(role);
	}

	/**
	 * 根据id查询菜单树
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/getRights")
	public List<Map<String, Object>> getDepart(String id) {
		// // 1.根据角色id获取该角色已有的权限集合
		// // 2.获取所有权限列表
		// // 3.拼接前台ztree需要的数据格式并且利用2分法判断并且选中已有的权限
		// // 查询角色已有的权限信息
		// ResponseMessage responseMessage = roleService.getSysRightsByRoleId(id);
		// List<SysRights> SysRightss = (List<SysRights>) responseMessage.getData();
		// int[] arr = new int[SysRightss.size()];
		// if (responseMessage.getSuccess()) {
		// for (int i = 0; i < SysRightss.size(); i++) {
		// arr[i] = SysRightss.get(i).getId();
		// }
		// // 将arr数组冒泡排序
		// for (int k = 0; k < arr.length - 1; k++) {
		// for (int j = k + 1; j < arr.length; j++) { // 升序把<改成>
		// if (arr[k] > arr[j]) {
		// int temp = arr[k];
		// arr[k] = arr[j];
		// arr[j] = temp;
		// }
		// }
		// }
		// }
		// // 获取所有权限列表
		// List<SysRights> SysRights = (List<com.yorma.common.po.SysRights>)
		// rightsService
		// .search(new Search().addFilterEqual("authType", 2)).getData();
		// List<Map<String, Object>> list = new ArrayList<>();
		// if (SysRights.size() != 0) {
		// // 迭代拼装权限菜单数据
		// for (SysRights el : SysRights) {
		// Map<String, Object> map = new HashMap<String, Object>();
		// map.put("id", el.getId());
		// map.put("pId", el.getParentId());
		// map.put("name", el.getName());
		// map.put("checked", search(arr, el.getId()));
		// list.add(map);
		// }
		// }
		//
		// return list;
		return null;
	}

	/**
	 * 更新角色权限
	 * 
	 * @param rightsId
	 *            权限集合
	 * @param roleId
	 *            角色Id
	 */
	@RequestMapping("/saveRights")
	public ResponseMessage saveRights(int[] rightsId, int roleId) {
		// List<Integer> rightsIds = new ArrayList<Integer>();
		// for (int el : rightsId) {
		// rightsIds.add(el);
		// }
		// ResponseMessage responseMessage = roleService.saveSysRoleRights(roleId,
		// rightsIds);
		// if (responseMessage.getSuccess()) {
		// UrlMetadataSourceService.clearSysRightMap();
		// }
		// return responseMessage;
		return null;

	}

	/**
	 * 2分法方法查找
	 * 
	 * @param arr
	 * @param key
	 * @return
	 */
	public boolean search(int[] arr, int key) {
		int start = 0;
		int end = arr.length - 1;
		while (start <= end) {
			int middle = (start + end) / 2;
			if (key < arr[middle]) {
				end = middle - 1;
			} else if (key > arr[middle]) {
				start = middle + 1;
			} else {
				return true;
			}
		}
		return false;
	}

	/**
	 * 删除角色
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public boolean delete(int[] ids) {
		if (ids.length != 0) {
			for (int i = 0; i < ids.length; i++) {
				sysRoleService.delete(ids[i]);
			}
			return true;
		} else {
			return false;
		}
	}

	@RequestMapping("/list")
	public ResponseMessage getRoles(int page, int rows, String name) {
		// Search search = new Search().setPage(page - 1).setMaxResults(rows);
		// if (StringUtils.isNotBlank(name)) {
		// search.addFilterEqual("name", name);
		// }
		// return roleService.search(search);
		return sysRoleService.getSysRoleList(page, rows, name);
		// return new ResponseMessage(true, new ResponseData());
	}
}
